<?php

/**
 * descript:php excel服务
 * 依赖于phpoffice/phpspreadsheet插件
 * User: blue
 * Date: 2018/8/27 16:01
 */

namespace rlibs\composer;

class Office
{
    public function __construct()
    {
       //存储路径
        $path=config('upload.excel_path');
        is_dir($path)||mkdir($path,0777,true);
    }

    /**
     * @param $workBook 工作簿对象
     * @param $type 浏览器输出文件类型
     * @param string $filename 浏览器输出文件名
     * @return bool
     */
    public function dataToBrowser($workBook,$type,$filename="默认文档"){
        if($workBook) return false;
        header('Cache-Control: max-age=0');
        if($type=="xlsx"){
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename='.$filename.'.xlsx');
            $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($workBook, 'Xlsx');
        }elseif($type=="xls"){
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename='.$filename.'xls');
            $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($workBook, 'Xls');
        }else{
            return false;
        }
        $writer->save('php://output');
    }
}